package com.pi2e.view.adapter;

import java.util.ArrayList;
import java.util.List;

import com.pi2e.R;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RoomNumAdapter extends RecyclerView.Adapter {

	//先定义两个ItemViewType，0代表头，1代表表格中间的部分
	  private static final int ITEM_VIEW_TYPE_HEADER = 0;
	  private static final int ITEM_VIEW_TYPE_ITEM = 1;
	  private Context context ;
	  //记录选择的房间号数字坐标
	  List<Integer> roomNumIndex = new ArrayList<Integer>() ;
	  int currentIndex = 0;
	  private OnLinearLayoutClickListener onLinearLayoutClickListener ;
	  //数据源
	  private List<String> dataList;
	 
	  /**
	   * 判断当前position是否处于第一个
	   * @param position
	   * @return
	   */
	  public boolean isHeader(int position) {
	    return position == 0;
	  }
	public RoomNumAdapter(List<String> dataList,Context context) {
		super();
		this.dataList = dataList;
		this.context = context ;
	}
	
	

	 @Override
	  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
	    //在onCreateViewHolder方法中，我们要根据不同的ViewType来返回不同的ViewHolder
	   /* if (viewType == ITEM_VIEW_TYPE_HEADER) {
	      //对于Header，我们应该返回填充有Header对应布局文件的ViewHolder（再次我们返回的都是一个布局文件，请根据不同的需求做相应的改动）
	      return new HeaderViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item_roomnum, null));
	    } else {*/
	      //对于Body中的item，我们也返回所对应的ViewHolder
	      return new BodyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item_roomnum, null));
	    //}
	  }
	 
	  @Override
	  public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
	   /* if (isHeader(position)) {
	      //大家在这里面处理头，这里只有一个TextView，大家可以根据自己的逻辑做修改
	      ((HeaderViewHolder)viewHolder).getTextView().setText("This is the Header!!");
	    }else {*/
	      //其他条目中的逻辑在此
		  ((BodyViewHolder) viewHolder).getTextView().setText(dataList.get(position));
		  ((BodyViewHolder) viewHolder).getLinearLayout().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			if (position == 9) {
				if (roomNumIndex.size()>0) {
					roomNumIndex.remove(roomNumIndex.size()-1) ;
				}
				
				onLinearLayoutClickListener.plusNum(getRoomNum(roomNumIndex)) ;
				//删除
			}else if(position == 11){
				//好了
				onLinearLayoutClickListener.complement();
			}else{
				//加
				roomNumIndex.add(position) ;
				onLinearLayoutClickListener.addNum(getRoomNum(roomNumIndex)) ;
			}
			
			}
		});
	     
	      
	   // }
	  }
	 
	  /**
	   * 总条目数量是数据源数量+1，因为我们有个Header
	   * @return
	   */
	  @Override
	  public int getItemCount() {
	    return dataList.size() ;
	  }
	 
	  /**
	   *
	   * 复用getItemViewType方法，根据位置返回相应的ViewType
	   * @param position
	   * @return
	   */
	  @Override
	  public int getItemViewType(int position) {
		  return 0 ;
	    //如果是0，就是头，否则则是其他的item
	   // return isHeader(position) ? ITEM_VIEW_TYPE_HEADER : ITEM_VIEW_TYPE_ITEM;
	  }
	 
	  /**
	   * 给头部专用的ViewHolder，大家根据需求自行修改
	   */
	  public class HeaderViewHolder extends RecyclerView.ViewHolder {
	    private TextView textView;
	    public HeaderViewHolder(View itemView) {
	      super(itemView);
	      textView = (TextView) itemView.findViewById(R.id.item_tv);
	    }
	    public TextView getTextView() {
	      return textView;
	    }
	  }
	 
	  /**
	   * 给GridView中的条目用的ViewHolder，里面只有一个TextView
	   */
	  public class BodyViewHolder extends RecyclerView.ViewHolder {
	    private TextView textView;
	    private LinearLayout linearLayout ;
	    public BodyViewHolder(View itemView) {
	      super(itemView);
	      textView = (TextView) itemView.findViewById(R.id.item_tv);
	      linearLayout = (LinearLayout) itemView.findViewById(R.id.line);
	    }
	    public TextView getTextView() {
	      return textView;
	      
	    }
	    public LinearLayout getLinearLayout(){
	    	return linearLayout ;
	    }
	  }
	public interface OnLinearLayoutClickListener{
		
		boolean addNum(String roomNum) ;
		boolean plusNum(String roomNum) ;
		void complement() ;
	}
	public OnLinearLayoutClickListener getOnLinearLayoutClickListener() {
		return onLinearLayoutClickListener;
	}
	public void setOnLinearLayoutClickListener(
			OnLinearLayoutClickListener onLinearLayoutClickListener) {
		this.onLinearLayoutClickListener = onLinearLayoutClickListener;
	}
	
	private String getRoomNum(List<Integer> mList){
		String tempNum = "" ;
		if (mList.size() == 0) {
			return "" ;
		}
		for (int i = 0; i < mList.size(); i++) {
			tempNum+= dataList.get(mList.get(i)) ;
		}
		return tempNum ;
			
	}
	
}
